package entidadesBD;

import java.io.Serializable;
import java.sql.Date;
import java.util.Collection;
import java.util.HashSet;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import entidadesVO.ArticulosOFADVO;
import entidadesVO.OFADVO;

@Entity
@Table(name = "ofad")
public class OFAD implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Date fechaOFAD;
	private Collection<ArticulosOFAD> articulosOFAD = new HashSet<ArticulosOFAD>();
	
	private int codigoOFAD;
	
	public OFAD(Date fechaOFAD) {
		this.fechaOFAD = fechaOFAD;
	}
	public OFAD(){
		//Constructor Vacio
	}
	
	@OneToMany(mappedBy = "ofad", fetch = FetchType.EAGER, cascade={CascadeType.ALL})
	public Collection<ArticulosOFAD> getArticulosOFAD() {
		return articulosOFAD;
	}
	public void setArticulosOFAD(Collection<ArticulosOFAD> articulosOFAD) {
		this.articulosOFAD = articulosOFAD;
	}
	public void addArticulosOFAD(ArticulosOFAD articulosOFAD){
		this.articulosOFAD.add(articulosOFAD);
	}
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getCodigoOFAD() {
		return codigoOFAD;
	}
	public void setCodigoOFAD(int codigoOFAD) {
		this.codigoOFAD = codigoOFAD;
	}
	
	@Column
	public Date getFechaOFAD() {
		return fechaOFAD;
	}
	public void setFechaOFAD(Date fechaOFAD) {
		this.fechaOFAD = fechaOFAD;
	}
	
	@Transient
	public OFADVO getOFADVO(){
		
		Collection<ArticulosOFADVO> articulosOFADVO = new HashSet<ArticulosOFADVO>();
		
		Object[] articulosOFADArray = articulosOFAD.toArray();
		
		for (int i = 0; i < articulosOFADArray.length; i++){
			ArticulosOFADVO aOVO = new ArticulosOFADVO();
			ArticulosOFAD aO = (ArticulosOFAD) articulosOFADArray[i];
			aOVO = aO.getArticulosOFADVO();
			articulosOFADVO.add(aOVO);
		}
		
		OFADVO ofadVO = new OFADVO(fechaOFAD);
		ofadVO.setArticulosOFAD(articulosOFADVO);
		ofadVO.setCodigoOFAD(codigoOFAD);
		
		return ofadVO;
	}
	
	public void setOFADVO(OFADVO ofadVO){
		
		this.fechaOFAD = ofadVO.getFechaOFAD();
	}
}
